﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _19直线导圆角功能
{
    public class Class1
    {

        [CommandMethod("FilletDemo")]
        public void FilletDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Line line1;
            Line line2;
            
            PromptEntityResult per = ed.GetEntity("选择第一条直线");
            if (per.Status != PromptStatus.OK) return;
            ObjectId line1Id = per.ObjectId;
            line1 = per.ObjectId.GetCopyLine();
            per = ed.GetEntity("选择第二条直线");
            if (per.Status != PromptStatus.OK) return;
            ObjectId line2Id = per.ObjectId;
            line2 = per.ObjectId.GetCopyLine();

            PromptDoubleResult pdr = ed.GetDouble("请输入倒角的半径");

            if (pdr.Status != PromptStatus.OK) return;
            
            double radius = pdr.Value;
            if (radius <= 0) return;
            //如果两条直线平行则不处理倒角
            if (line1.GetAngleFromTwoLines(line2) == Math.PI || line1.GetAngleFromTwoLines(line2) == 0) return;
            //Arc arc = new Arc();
            //bool isHasArc = line1.GetArcFrom2Lines(line2,5,out arc);
            //if (isHasArc)
            //{
            //    db.AddEntiy(arc);
            //}
            List<Entity> ents = line1.GetArcFrom2Lines(line2, radius);
            if (ents.Count >1)
            {
                db.AddEntiy(ents);
                db.DeleteEntity(line1Id);
                db.DeleteEntity(line2Id);
            }
        }

        [CommandMethod("Test")]
        public void Test()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptEntityResult per = ed.GetEntity("选择一条直线");
            if (per.Status == PromptStatus.OK)
            {
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    Line line = (Line)per.ObjectId.GetObject(OpenMode.ForRead);
                    ed.WriteMessage(line.Angle.angleToDegree().ToString());
                }
            }
        }
    }
}
